package com.sdleyou.treepro.common.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sdleyou.treepro.common.model.dao.UserDO;
import com.sdleyou.treepro.common.service.PassportManager;
import com.sdleyou.treepro.system.cache.Cache;
import com.sdleyou.treepro.system.security.model.JWTConstant;
import com.sdleyou.treepro.system.security.model.Role;
import com.sdleyou.treepro.system.security.model.User;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 会员账号管理实现
 *
 * @author zh
 * @version v7.0
 * @since v7.0
 * 2018-04-8 11:33:56
 */
@Service
public class PassportManagerImpl implements PassportManager {


    @Autowired
    private Cache cache;
    @Autowired
    private PassportManager passportManager;

    @Override
    public String createToken(UserDO userDO, Integer time) {
        User user = new User();
        BeanUtils.copyProperties(userDO, user);
        ObjectMapper oMapper = new ObjectMapper();
        String token = null;
        //获取用户信息
        Map buyerMap = oMapper.convertValue(user, HashMap.class);
        token = Jwts.builder()
                .setClaims(buyerMap)
                .setSubject(Role.USER.name())
                .setExpiration(new Date(System.currentTimeMillis() + time * 1000))
                .signWith(SignatureAlgorithm.HS512, JWTConstant.SECRET)
                .compact();
        return token;
    }
}

